<?php
#by reemo 2012
class ShowDmtechPage extends AbstractPage
{	
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	

	private function calcTM($Element)
	{
		global $PLANET, $USER, $resource, $pricelist, $CONF;
		
		$elementLevel = $USER[$resource[$Element]];
		
		$metal 		= ceil(($pricelist[$Element]['cost']['901'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.00025);
		$crystal	= ceil(($pricelist[$Element]['cost']['902'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.0005);
		$deiterium 	= ceil(($pricelist[$Element]['cost']['903'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.001);
		$nrg	 	= ceil(($pricelist[$Element]['cost']['911'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 0.05);
		$tm	 		= ceil(($pricelist[$Element]['cost']['921'] * pow($pricelist[$Element]['factor'], $elementLevel)) * 1);
		
		$nac 		= $CONF['resource_multiplier'] < 50 ? 1.2 : ($CONF['resource_multiplier'] < 500 ? 1.05 : 0.4);
		
		$cost 		= ceil($nac * max(1,($metal + $crystal + $deiterium + $tm + $nrg)));
	
		return($cost);
	}
	
	function up()
	{
		global $PLANET, $USER, $resource, $reslist;
		
		$Element     		= HTTP::_GP('tech', 0);
		$Elements			= $reslist['tech'];
		$costTM =  $this->calcTM($Element);
		if (!in_array($Element,$Elements) || $USER['b_tech_id'] == $Element || $costTM > $USER['darkmatter'] || !BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element) || $Element == 199)
		{
			$this->redirectTo('game.php?page=dmtech');
			return;
		}
		if($Element == 125 && $CONF['resource_multiplier'] > 50)
		{
			$this->redirectTo('game.php?page=dmtech');
			return;
		}
		
		$USER['darkmatter'] -= $costTM;

		$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET
				 `".$resource[$Element]."` = `".$resource[$Element]."` + 1
				 WHERE `id` = '".$USER["id"]."';");		
		
		#$this->printMessage('Успешно улучшено!',true,  array("game.php?page=dmbuild", 1));
		$this->redirectTo('game.php?page=dmtech');
	}
	
		
	public function show()
	{
		global $resource, $reslist, $CONF, $PLANET, $USER, $pricelist;
	
		$BuildInfoList      = array();

		$Elements			= $reslist['tech'];;
		
		foreach($Elements as $ID => $Element)
		{
			if(!BuildFunctions::isTechnologieAccessible($USER, $PLANET, $Element) || $USER['b_tech_id'] == $Element || $Element == 199)
				continue;
			if($Element == 125 && $CONF['resource_multiplier'] > 50)
				continue;
				
			$costTM =  $this->calcTM($Element);
			$BuildInfoList[$Element]	= array(
				'level'				=> $USER[$resource[$Element]],
				'maxLevel'			=> $pricelist[$Element]['max'],
				'costTM'			=> $costTM,
			);
		}

		
		
		$this->tplObj->assign_vars(array(
			'BuildInfoList'			=> $BuildInfoList,
		));
			
		$this->display('page.dm.tech.tpl');
	}
}
?>